#!/bin/bash

function process_drc_log {
cat drc.log | grep STATS | sed "s/\(.*\)STATS: \(.*\)/\2/; s/=/ /g" | \
sort -n -k2 \
| awk 'BEGIN{first=1; slot=0; nslot=0;items=0; loops=0; rps=0; latency=0; \
             nslots_count=0; slots_count=0; all_slots=0; all_items=0; all_loops=0; all_rps=0; all_latency=0; }   \
 { \
  if ((first == 1) || (slot == $2)) { \
    #  printf "(slot=%d cur=%d) first=%d\n", slot, $2, first; \
      nslot+=1; items+=$4; loops+=$6; rps+=$8; latency+=$10; first=0; slot=$2;  \
   }   \
   else {   \
       printf "each: slot    %4d nslot   %5d items   %8d     loops%8d    rps %4d     latency %8d\n",                \
                slot, nslot, items/nslot, loops/nslot, rps/nslot, latency/nslot;                                    \
       nslots_count++; slots_count+=nslot; all_slots+=nslot; all_items+=items; all_loops+=loops; all_rps+=rps; all_latency+=latency;      \
       slot=$2; nslot=1; items=$4; loops=$6; rps=$8; latency=$10; \
   }  \
  } \
  END{printf "each: slot    %4d nslot   %5d items   %8d     loops%8d    rps %4d     latency %8d\n", \
                slot, nslot, items/nslot, loops/nslot, rps/nslot, latency/nslot; all_slots++; \
      nslots_count++; slots_count+=nslot; all_slots+=nslot; all_items+=items; all_loops+=loops; all_rps+=rps; all_latency+=latency;    \
      printf "all:  slot    %4d avgslot %5d avgitems %8d  avgloops %8d  avgrps %4d  avglatency %8d\n",  \
              nslots_count, all_slots/nslots_count, all_items/slots_count, all_loops/slots_count, all_rps/slots_count, all_latency/slots_count; }      \
' | tee rec_inner.log
}


<<HICKEY
mysql -umonitor -ptb4monitor -h172.24.64.233 beidou
CREATE TABLE drc_kpi (
        service varchar(30) DEFAULT NULL,
        items decimal(14,2) DEFAULT NULL,
        loops decimal(14,2) DEFAULT NULL,
        latency decimal(14,2) DEFAULT NULL,
        rps decimal(14,2) DEFAULT NULL,
        gmt_create datetime DEFAULT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8

CREATE TABLE drc_kpi_detail (
        service varchar(30) DEFAULT NULL,
        slot decimal(14,2),
        nslot decimal(14,2),
        items decimal(14,2) DEFAULT NULL,
        loops decimal(14,2) DEFAULT NULL,
        latency decimal(14,2) DEFAULT NULL,
        rps decimal(14,2) DEFAULT NULL,
        gmt_create datetime DEFAULT NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8

Data demo:

each: slot       0 nslot      74 items          0     loops      36    rps    0     latency  1001573
each: slot       1 nslot     669 items          2     loops     330    rps    0     latency  1006922
each: slot       2 nslot      74 items          0     loops      36    rps    0     latency   999241
each: slot       3 nslot     676 items          2     loops     328    rps    0     latency  1005983
each: slot       4 nslot      74 items          0     loops      36    rps    0     latency   995581
each: slot       5 nslot     678 items          2     loops     328    rps    0     latency  1005219
each: slot       6 nslot      74 items          0     loops      36    rps    0     latency   996401
each: slot       7 nslot     676 items          2     loops     327    rps    0     latency  1004552
all:  slot       8 avgslot   374 avgitems        2  avgloops      299  avgrps    0  avglatency  1004928

HICKEY

function insert_records {
lines=`wc -l rec_inner.log | awk '{print $1}'`
cur_line=1
while read line
do    
    #echo "$line" | read arr[1] arr[2] arr[3]
    #echo ${arr[1]} ${arr[2]} ${arr[3]}

    arr=(`echo "$line" | tr -s ' '`)
    echo ${arr[@]}
    echo ${arr[2]} ${arr[4]} ${arr[6]} ${arr[8]} ${arr[10]} ${arr[12]}
    
    if [ $cur_line -ne $lines ];then
        table_name="drc_kpi_detail"
        cmd="insert into $table_name(service, slot, nslot, items, loops, rps, latency) values('drc_zds', ${arr[2]},  ${arr[4]}, ${arr[6]},  ${arr[8]}, ${arr[10]}, ${arr[12]});"

    else 
        table_name="drc_kpi"
        cmd="insert into $table_name(service, items, loops, rps, latency) values('drc_zds', ${arr[6]},  ${arr[8]}, ${arr[10]}, ${arr[12]});"
    fi   
 
    echo "cmd=$cmd"
    mysql -umonitor -ptb4monitor -h172.24.64.233 beidou -e "$cmd"
   let cur_line+=1

done < "rec_inner.log"
}

while [ 1 ]
do
    process_drc_log
    insert_records
    sleep 300
done    


